import moment from 'moment'
import qs from 'qs'

function getQsParams(params) {
    if (params) return qs.stringify(params)
}

export default {
    // 时间格式化
    timeFormat(date, arg) {
        let time = date || new Date(),
            param = arg || 'YYYY-MM-DD HH:mm:ss'
        return moment(time).format(param)
    },
    // 数字格式化
    numFormat(date, arg) {
        return parseInt(date).toLocaleString()
    },
    listFilter(list, arg, name, value) {
        let result = null
        list.forEach((item, index) => {
            if (item[name] === arg) {
                result = item[value]
            }
        })
        return result
    },
    getQsParams,
    appendUrlParams(url, param) {
        let params
        if (Object.prototype.toString.call(param) === '[object Object]') {
            params = getQsParams(param)
        } else {
            params = param
        }
        if (url.indexOf('?') >= 0) {
            return url + '&' + params
        } else {
            return url + '?' + params
        }
    }
}
